using System;
using System.Collections.Generic;
using ScrumManagementTool.db.interfaces;

namespace ScrumManagementTool.db
{
    public class DbRegistration : IDbRegistration
    {
        private IDbGateway dbGateway;

        public DbRegistration():this(new DbGateway())
        {
            
        }

        public DbRegistration(IDbGateway dbGateway)
        {
            this.dbGateway = dbGateway;
        }

        public bool CheckUsernameAvailability(string username)
        {
            string stmt = "SELECT id FROM members WHERE username=:username";

            IDictionary<string, string> values = new Dictionary<string, string>();
            values.Add(new KeyValuePair<string, string>(":username", username));

            return !dbGateway.ExecuteSelectForBool(stmt, values);
        }

        public void AddUser(string username, string password, string contactNo, string name, string email)
        {
            string stmt = "INSERT INTO members (username, password, contactnumber, name, email) " +
                          "VALUES (:username, :password, :contactnumber, :name, :email)";

            IDictionary<string, string> values = new Dictionary<string,string>();
            values.Add(new KeyValuePair<string, string>(":username", username));
            values.Add(new KeyValuePair<string, string>(":password", password));
            values.Add(new KeyValuePair<string, string>(":contactnumber", contactNo));
            values.Add(new KeyValuePair<string, string>(":name", name));
            values.Add(new KeyValuePair<string, string>(":email", email));

            dbGateway.ExecuteInsertStatement(stmt, values);
        }
    }
}